package com.danton.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

@SuppressWarnings("serial")
public class BsRoleModel extends Model<BsRoleModel>{
	public static final BsRoleModel dao = new BsRoleModel();
	
	public List<BsRoleModel> findRolesByUserID(int user_id){
		return super.find("select br.* from bs_role br join bs_admin_role bar on br.id=bar.role_id where bar.admin_id=? and br.status=0", user_id);
	}
	public List<BsRoleModel> findRoleNameByUserID(int user_id){
		return super.find("select br.name as role_name from bs_role br join bs_admin_role bar on br.id=bar.role_id where bar.admin_id=? and br.status=0", user_id);
	}
	
	public BsRoleModel findRoleByRoleCode(String role_code){
		return super.findFirst("select * from bs_role where code=?", role_code);
	}
	public BsRoleModel getRoleById(int role_id){
		return super.findFirst("select * from bs_role where id=?", role_id);
	}
	/*
	 * 查询角色是否冻结
	 * true:冻结,false,反之
	 * */
	public boolean roleStatusCheck(int role_id){
		 BsRoleModel role = super.findFirst("select * from bs_role where id="+role_id);
		 int status = role.getInt("status");
		 if(status==1){
			 return true;
		 }
		 return false;
	}
	
	/*
	 * 查询所有角色
	 * */
	public List<BsRoleModel> listAllRoles(){
		return super.find("select * from bs_role where status=0");
	}
	/*
	 * 查询用户已关联角色
	 * */
	public List<BsRoleModel> listUserRoles(int user_id){
		return super.find("select bar.*,br.name as role_name from bs_role br join bs_admin_role bar on br.id=bar.role_id where bar.admin_id=? and br.status=0", user_id);
	}
	public Page<BsRoleModel> paginate(int pageNumber, int pageSize){
		return super.paginate(pageNumber, pageSize, "select *,id as role_id", "from bs_role order by create_time desc");
	}
	
	public Page<BsRoleModel> paginateByKw(int pageNumber, int pageSize,String kw){
		String likeKw = "%"+kw+"%";
		return super.paginate(pageNumber, pageSize, "select *,id as role_id",
				"from bs_role where id=? or name like ? order by create_time desc", kw, likeKw);
	}
}
